<?php
namespace Ucmaim\Controller\Admin;

class OrderController extends CommonController{

    public function _initialize(){
        parent::_initialize();
    }

    public function Index(){
        $Order = D('Order');
        $condition = I('post.', '', 'trim');
        if( ! empty($condition) ){
            session('search', $condition);
        }elseif( ! I('get.Page') ){
            session('search', null);
        }
        $condition = session('search') ? session('search') : [];
        $list = $Order->getAllOrders($condition);

        $status = C('ORDER_STATUS');
        $status_text = [];
        foreach($status as $k => $v){
            $status_text[$k] = array('text' => $v);
        }

        if(!empty($condition['status'])){
            if( count($condition['status']) === 1 && $condition['status'][0] == -5 ){
                $this->assign('ShowClearStorageBtn', true);
            }

            foreach($condition['status'] as $k => $v){
                if( isset($status_text[$v]) ){
                    $status_text[$v]['checked'] = 'checked="checked"';
                }
            }
        }

        $list = gotoPage($list, $this->pageSize);
        $this->assign('search', $condition);
        $this->assign('status', $status_text);
        $this->assign('list', $list['data']);
        $this->assign('page', $list['page']);
        $this->display();
    }

    public function Modify(){
        $Order = D('Order');
        $status_list = C('ORDER_STATUS');
        if(IS_POST){
            $post = I('post.');
            $sn = $post['sn'];
            $status = (int)$post['status'];
            if(! isset($status_list[$status])) $this->error('非法操作');
            $order_info = $Order->getOrderInfo($sn);
            if(empty($order_info)) $this->error('该订单不存在');
            if( $status == 0 ){    // 订单已完成，标识已打款，增加卖家资金流水，扣除商品库存
                $Order->_balance($order_info) ? $this->success('操作成功', U('Admin/Order/Index')) : $this->error('操作失败，请稍候再试', U('Admin/Order/Index'));
            }elseif( $status == -1){
                // 取消订单，需要释放商品的冻结库存 - 1. 付款超时 2. 发货超时
                $Goods = D('Goods');
                if( $order_info['order_real_status'] == -5 && $order_info['order_status'] == 1 ){
                    // 付款超时， 释放冻结库存
                    $Order->startTrans();
                    $r1 = $Order->updateOrderStatus($sn, -1, '因订单支付超时，订单已被消失');   // 更新订单状态为取消
                    $r2 = $Goods->changeStorage($order_info['goods_id'], $order_info['order_number'], 'release');  // 释放商品库存
                    if($r1 && $r2){
                        $Order->commit();
                        $this->success('操作成功', U('Admin/Order/Index'));
                    }else{
                        $Order->rollback();
                        $this->error('操作失败，请稍候再试', U('Admin/Order/Index'));
                    }
                }elseif( $order_info['order_real_status'] == -2 && $order_info['order_status'] == 2 ){
                    // 发货超时，释放冻结库存，打款给卖家
                    $Order->startTrans();
                    $r1 = $Order->updateOrderStatus($sn, -1, '因卖家发货超时，订单已被取消');   // 更新订单状态为取消
                    $r2 = $Goods->changeStorage($order_info['goods_id'], $order_info['order_number'], 'release');  // 释放商品库存
                    if($r1 && $r2){
                        $Order->commit();
                        $this->success('操作成功', U('Admin/Order/Index'));
                    }else{
                        $Order->rollback();
                        $this->error('操作失败，请稍候再试', U('Admin/Order/Index'));
                    }

                }
            }else{
                $result = $Order->updateOrderStatus($sn, $status);
                $result ? $this->success('更新成功') : $this->error('更新失败');
            }
            exit();
        }

        $sn = I('get.sn');
        $order_info = $Order->getOrderInfo($sn);
        if(empty($order_info)) $this->error('该订单不存在');
        $goods_info = D("Goods")->getGoodsInfo($order_info['goods_id'], false);

        $this->assign('goods', $goods_info);
        $this->assign('order', $order_info);
        $this->assign('status', $status_list);
        $this->display();
    }

    // 支付超时订单取消，需要释放冻结库存
    public function ClearStorageBtn(){
        $Order = D('Order');
        $Goods = D('Goods');
        // 获取支付超时的订单数据
        $list = $Order->get(0, 'all', $status = -5);
        if( !empty($list) ){
            $Order->startTrans();
            $error = [];
            foreach( $list as $k => $v ){
                $goods_id = $v['goods_id'];
                $amount   = $v['order_number'];
                $order_sn = $v['order_sn'];
                // 更新订单为取消状态
                $flag1 = $Order->updateOrderStatus($order_sn, -1);
                // 释放商品库存
                $flag2 = $Goods->changeStorage($goods_id, $amount, 'release');
                if( ! $flag1 || ! $flag2 ){
                    $error[] = $order_sn;
                }
            }

            if( empty($error) ){
                $Order->commit();
                $this->success('操作已成功', U('Admin/Order/Index'));
            }else{
                $Order->rollback();
                $message = '以下订单处理异常，本次操作将不会造成任何改动<br />['.implode(',', $error).']';
                $this->success($message, U('Admin/Order/Index'));
            }

        }
    }
}